package eai.tp2;

import eai.tp2.core.CameraListReader;
import eai.tp2.core.CameraSummaryWriter;
import eai.tp2.core.SummaryEngine;
import eai.tp2.core.datamodel.CameraSummary;
import eai.tp2.utils.User;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.JDOMException;
/**
 * This class is the main of the application. It reads the brand or brands and process 
 * the xml files to summarize for each brand.
 * 
 * 
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        System.setSecurityManager(null);
        // TODO Auto-generated method stub
        Main console = new Main();
        console.start();
    }

    public void start() {
        while (true) {
            System.out.println("Usage: <brand1> <brand2> (ex:canon epson)");
            System.out.print(prompt);
            String line = User.readString().trim();

            if (line.isEmpty()) {
                continue;
            }
            if (line.equalsIgnoreCase("quit")) {
                break;
            }


            String[] parameters = line.split("\\s+");
            CameraListReader listReader;
            try {
                listReader = new CameraListReader(parameters);
                SummaryEngine engine = new SummaryEngine();
                List<CameraSummary> cameras = engine.process(listReader.cameras);
                new CameraSummaryWriter(cameras).write();
            } catch (JDOMException ex) {
                System.out.println("[ERROR] XML file not valid. Please notice if the camera brand has models."+ex.getLocalizedMessage());
            } catch (IOException ex) {
                System.out.println("[ERROR] Please type the right camera XML file, or confirm that the XML file exist.");               
            }

        }

    }
    private String prompt = "\n$ ";
}
